#include "include/Acceptor.hpp"

void Acceptor::ready(){
    int lfd = fd();
    int reuse = 1;
    int errOA = setsockopt(lfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
    if(errOA == -1 ){
        perror("Acceptor::ready::setsockopt::SO_REUSEADDR");
    }
    int errOP = setsockopt(lfd,SOL_SOCKET,SO_REUSEPORT,&reuse,sizeof(reuse));
    if(errOP == -1){
        perror("Acceptor::ready::setsockopt::SO_REUSEADDR");
    }
    int errb = bind(lfd,_addr.getSockAddr(),sizeof(sockaddr_in));
    if(errb == -1){
        perror("Acceptor::ready::bind");
        return;
    }

    int errl = listen(lfd,1000);
    if(errl == -1){
        perror("Acceptor::ready::listen");
    }
}

int Acceptor::accept(){
    int afd = ::accept(fd(),nullptr,nullptr);
    if(afd== -1){
        perror("Acceptor::accept");
    }
    return afd;
}


